/*顶部菜单*/
(function ($) {
    $.fn.yqEditMenu = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqEditMenu.methods[opts](this, param);
        }

        return this.each(function () {
            var $this = $(this);
            var opt;
            if ($this.data("yqtype") == "yqEditMenu") {
                var $uniqueID = $this.attr("ID");
                var opts_ = $.extend({}, options($this));
                destroy($this);
                $this = $("#" + $uniqueID);
                opt = $.extend(opts_, opts);
            }
            else {
                opt = $.extend({}, $.fn.yqEditMenu.defaults, opts);
                var $uniqueID = $this.attr("ID");
                if ($uniqueID === undefined || $uniqueID === null || $uniqueID == "") {
                    $uniqueID = $.getUniqueID();
                    $this.attr("ID", $uniqueID);
                }
            }

            //变量信息
            $this.data("editmenu_opt", opt);
            $this.data("yqtype", "yqEditMenu");

            initmenu($this);
        });
    };

    $.fn.yqEditMenu.defaults = {
        ajaxUrl: "/_db.html", //ajax地址
        type: 1,  //1(!=2或3)-顶部菜单，2-右键菜单，3-顶部+右键
        mini: false,    //按钮是否最小化
        width: 150,     //右键菜单第一层宽度
        copy: false,
        addroot: false,
        append: false,
        add: true,
        upp: true,
        del: true,
        save: false,
        isdel: false,
        state: false,   //可状态编辑
        state_field: "state",//状态字段
        cut: false,
        move: false,
        toexcel: false,   //excel导出
        inexcel: false,   //excel导入
        links: [],   //左侧 显示的链接菜单
        menus: [],   //左侧 显示在其它中的菜单

        titles: [],
        ddSeeks: [],    //下拉查询字段，字段名称
        isSimple: true, //简单查询
        txtSeeks: [],   //简单查询字段，字段名称
        isSimpleDDLW: 120, //简单查询选择项目宽度
        isSimpleTxtW: 160, //简单查询输入值宽度

        isSeek: true,   //高级查询
        seekModeW: 500,
        seekModeH: 400,
        seek: [],       //查询条件

        links_r: [], //右侧 显示的链接菜单
        beforeBindingTitle: function (top_menus, pop_menus, callback) {
            callback(top_menus, pop_menus);
        }, //标题绑定前
        afterBindingTitle: function () { }, //标题绑定后
        beforeOpen: function (callback) {   //左侧右键菜单，进入菜单显时处理接口
            callback(true)
        },

        beforeOpen_r: function (callback) {   //右侧右键菜单，进入菜单显时处理接口
            callback(true)
        },

        onClick: function (action, params) {      //按钮点击事件，action为操作id

        },
        onSeek: function (where, order) {           //查询

        },

        addrootText: "AddRoot",
        copyText: "Copy",
        appendText: "Append",
        addText: "Add",
        uppText: "Upp",
        delText: "Del",
        isdel1Text: "Disable",
        isdel0Text: "Enable",
        stateText: "State",
        moveText: "Move",
        saveText: "Save",
        undoText: "Undo",
        toexcelText: "ToExcel",
        inexcelText: "InExcel",
        cutText: "Cut",
        uncutText: "UnCut",
        pasteRootText: "PasteRoot",
        pasteText: "Paste",
        otherText: "Other",
        searchText: "Search"
    };

    $.fn.yqEditMenu.methods = {
        options: function (jq) {
            return options(jq);
        },
        destroy: function (jq) {
            destroy(jq);
        },
        getBtns: function (jq, id) {
            return getBtns(jq, id);
        },
        enable: function (jq, id) {
            isEdit(jq, id, true);
        },
        disable: function (jq, id) {
            isEdit(jq, id, false);
        },
        show: function (jq, id) {
            isShow(jq, id, true);
        },
        hide: function (jq, id) {
            isShow(jq, id, false);
        },
        open: function (jq, e) {
            var opt = $(jq[0]).data("editmenu_opt");
            if (opt.type == 2 || opt.type == 3) {
                if (opt.pop_menus.length == 0) {
                    return;
                }

                $(jq[0]).yqPopMenu("open", e);
            }
        },
        open_seek: function (jq, e) {
            show_seek(jq, e);
        }
    };


    function options($object) {
        return $object.data("editmenu_opt");
    }

    function form_load($object) {
        var opt = options($object);
        opt.afterBindingTitle.call($object);
    }

    function destroy($object) {
        var opt = options($object);
        if (opt === undefined) {
            return;
        }

        if (typeof opt.menu_l != "undefined") {
            opt.menu_l.yqTopMenu("destroy");
        }

        if (typeof opt.menu_r != "undefined") {
            opt.menu_r.yqTopMenu("destroy");
        }

        if (opt.type == 2 || opt.type == 3) {
            if (opt.pop_menus.length > 0) {
                $object.yqPopMenu("destroy");
            }
        }


        opt.menu_l.remove();
        opt.menu_r.remove();

        $object.removeData("editmenu_opt");
        $object.removeData("yqtype");
        $object.empty();
    }

    function initmenu($object) {
        var opt = options($object);

        // $.yqAddCSS(`
        // .seektb_main,.seektb {
        //     margin: 0;
        //     padding: 0;
        //     border: 0;
        //     border-collapse: collapse; 
        // }

        // .seektb_main tr td,.seektb tr td {
        //     vertical-align: middle;
        //     text-align: center;
        //     font-size: 12px;
        //     padding: 0px;
        //     border: 1px solid #e6e6e6;
        // }

        // .seektb_main tr td {            
        //     color: #000000;
        //     font-weight: bolder;
        // }

        // .seektb tr td {            
        //     color: #333435;
        //     font-weight: normal;
        //     text-align: left;
        //     padding: 2px 1px;
        // }

        // .seektb tr td .select_order{
        //     border-width: 1px;
        //     border-color: #95b8e7;
        //     border-radius: 5px 5px 5px 5px;
        //     text-align: center;
        //     outline-style: none;
        // }

        // `, "editmenu_css");

        var top_menus = [];
        if (opt.addroot) {
            top_menus.push({
                id: "addroot",
                text: opt.addrootText,
                iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-home" : "yqwin-btn-icon-sub fa fa-home",
                onClick: function () {
                    opt.onClick.call($object, "addroot");
                }
            });
        }

        if (opt.copy) {
            top_menus.push({
                id: "copy",
                text: opt.copyText,
                iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-files-o" : "yqwin-btn-icon-sub fa fa-files-o",
                onClick: function () {
                    opt.onClick.call($object, "copy");
                }
            });
        }

        if (opt.append) {
            top_menus.push({
                id: "append",
                text: opt.appendText,
                iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-indent" : "yqwin-btn-icon-sub fa fa-indent",
                onClick: function () {
                    opt.onClick.call($object, "append");
                }
            });
        }
        if (opt.add) {
            top_menus.push({
                id: "add",
                text: opt.addText,
                iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-plus-square" : "yqwin-btn-icon-sub fa fa-plus-square",
                onClick: function () {
                    opt.onClick.call($object, "add");
                }
            });
        }

        if (opt.upp) {
            top_menus.push({
                id: "upp",
                text: opt.uppText,
                iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-pencil-square" : "yqwin-btn-icon-sub fa fa-pencil-square",
                onClick: function () {
                    opt.onClick.call($object, "upp");
                }
            });
        }

        if (opt.del) {
            top_menus.push({
                id: "del",
                text: opt.delText,
                iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-trash" : "yqwin-btn-icon-sub fa fa-trash",
                onClick: function () {
                    opt.onClick.call($object, "del");
                }
            });
        }

        if (opt.save) {
            if (top_menus.length > 0) {
                top_menus.push({});
            }
            top_menus.push({
                id: "save",
                text: opt.saveText,
                iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-floppy-o" : "yqwin-btn-icon-sub fa fa-floppy-o",
                onClick: function () {
                    opt.onClick.call($object, "save");
                }
            });
            top_menus.push({
                id: "undo",
                text: opt.undoText,
                iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-undo" : "yqwin-btn-icon-sub fa fa-undo",
                onClick: function () {
                    opt.onClick.call($object, "undo");
                }
            });
        }

        if (opt.links.length > 0) {
            // if (top_menus.length > 0) {
            //     top_menus.push({});
            // }
            opt.links.forEach(link => {
                if (typeof link.id != "undefined") {
                    if (typeof link.onClick != "function") {
                        link.onClick = function () {
                            opt.onClick.call($object, link.id);
                        }
                    }

                    top_menus.push(link);
                } else {
                    top_menus.push({});
                }
            });
        }

        var pop_menus = $.extend([], top_menus);

        var addmenus = [];
        if (opt.isdel) {
            addmenus.push({
                id: "isdel1",
                text: opt.isdel1Text,
                iconCls: "yqwin-btn-icon-sub fa fa-minus-circle",
                onClick: function () {
                    opt.onClick.call($object, "isdel1");
                }
            });
            addmenus.push({
                id: "isdel0",
                text: opt.isdel0Text,
                iconCls: "yqwin-btn-icon-sub fa fa-caret-square-o-right",
                onClick: function () {
                    opt.onClick.call($object, "isdel0");
                }
            });
        }

        if (opt.cut) {
            if (addmenus.length > 0) {
                addmenus.push({});
            }

            addmenus.push({
                id: "cut",
                text: opt.cutText,
                iconCls: "yqwin-btn-icon-sub fa fa-scissors",
                onClick: function () {
                    opt.onClick.call($object, "cut");
                }
            });
            addmenus.push({
                id: "uncut",
                text: opt.uncutText,
                iconCls: "yqwin-btn-icon-sub fa fa-chain-broken",
                onClick: function () {
                    opt.onClick.call($object, "uncut");
                }
            });
            addmenus.push({
                id: "pasteroot",
                text: opt.pasteRootText,
                iconCls: "yqwin-btn-icon-sub fa fa-clipboard",
                onClick: function () {
                    opt.onClick.call($object, "pasteroot");
                }
            });
            addmenus.push({
                id: "paste",
                text: opt.pasteText,
                iconCls: "yqwin-btn-icon-sub fa fa-file-o",
                onClick: function () {
                    opt.onClick.call($object, "paste");
                }
            });
        }

        if (opt.state) {
            var state_lists = [];
            var state_menus = [];
            for (var i = 0; i < opt.titles.length; i++) {
                if (opt.titles[i].name == opt.state_field) {
                    state_lists = opt.titles[i].ui_mask.lists;
                    break;
                }
            }
            if (state_lists === undefined) {
                state_lists = [];
            }
            for (var i = 0; i < state_lists.length; i++) {
                state_menus.push({
                    id: "state_" + state_lists[i].no,
                    text: state_lists[i].name,
                    data: state_lists[i],
                    onClick: function () {
                        opt.onClick.call($object, "state", $(this).data("extdata"));
                    }
                })
            }

            if (state_menus.length > 0) {
                addmenus.push({
                    id: "state",
                    text: opt.stateText,
                    iconCls: "yqwin-btn-icon-sub fa fa-cogs",
                    subs: state_menus
                });
            }
        }

        if (opt.toexcel) {
            addmenus.push({
                id: "toexcel",
                text: opt.toexcelText,
                iconCls: "yqwin-btn-icon-sub fa fa-file-excel-o",
                onClick: function () {
                    opt.onClick.call($object, "toexcel");
                }
            });
        }

        if (opt.inexcel) {
            addmenus.push({
                id: "inexcel",
                text: opt.inexcelText,
                iconCls: "yqwin-btn-icon-sub fa fa-table",
                onClick: function () {
                    opt.onClick.call($object, "inexcel");
                }
            });
        }

        if (opt.move) {
            if (addmenus.length > 0) {
                addmenus.push({});
            }

            addmenus.push({
                id: "move",
                text: opt.moveText,
                iconCls: "yqwin-btn-icon-sub fa fa-arrows",
                onClick: function () {
                    opt.onClick.call($object, "move");
                }
            });
        }

        if (opt.menus.length > 0) {
            opt.menus.forEach(menu => {
                if (typeof menu.id != "undefined") {
                    if (typeof menu.onClick != "function") {
                        menu.onClick = function () {
                            opt.onClick.call($object, menu.id);
                        }
                    }
                    addmenus.push(menu);
                } else {
                    addmenus.push({});
                }
            });
        }

        if (addmenus.length == 1) {
            if (top_menus.length > 0) {
                top_menus.push({});
            }
            top_menus.push(addmenus[0]);
        }
        else if (addmenus.length > 1) {
            var other = {
                id: "other",
                text: opt.otherText,
                subs: addmenus
            };
            if (opt.mini) {
                other.iconCls = "yqwin-btn-icon fa fa-ellipsis-v";
            }

            top_menus.push(other);
        };

        if (addmenus.length > 0) {
            pop_menus.push("{}");
            for (var i = 0; i < addmenus.length; i++) {
                pop_menus.push($.extend({}, addmenus[i]));
            }
        }

        if (opt.type != 2) {
            var top_menus_r = [];
            if (opt.titles.length > 0) {
                if (opt.isSimple) {
                    if (opt.txtSeeks.length == 0) {
                        for (var i = 0; i < opt.titles.length; i++) {
                            if (opt.titles[i].ui_visible &
                                (opt.titles[i].ui_type == 1
                                    || opt.titles[i].ui_type == 2
                                    || opt.titles[i].ui_type == 5)) {
                                opt.txtSeeks.push(opt.titles[i].name);
                            }
                        }
                    }

                    // 下拉查询              
                    if (opt.ddSeeks.length > 0) {
                        for (var j = 0; j < opt.ddSeeks.length; j++) {
                            for (var i = 0; i < opt.titles.length; i++) {
                                if (opt.titles[i].name == opt.ddSeeks[j]) {
                                    var com_data = opt.titles[i].ui_mask.lists;
                                    com_data.unshift({
                                        no: -1,
                                        name: "All"
                                    });
                                    console.log("opt.titles[i]", opt.titles[i]);
                                    const labelWidth = opt.titles[i].ui_title.length * 18;
                                    top_menus_r.push({
                                        id: opt.ddSeeks[j],
                                        type: "combobox",
                                        editable: false,
                                        label: opt.titles[i].ui_title,
                                        labelPosition: "before",
                                        labelWidth: labelWidth,
                                        width: (opt.titles[i].ui_width + labelWidth),
                                        panelWidth: (opt.titles[i].ui_width),
                                        panelHeight: (33 * com_data.length),
                                        panelMaxHeight: 300,
                                        valueField: 'no',
                                        textField: 'name',
                                        data: com_data,
                                        value: -1,
                                        s_field: opt.ddSeeks[j],
                                        onChange: function (e) {
                                            var name = $(this).combobox("options").s_field;
                                            var val = $(this).combobox("getValue");
                                            var where = "";
                                            if (!(val === null || val == -1)) {
                                                where = name + " = " + val;
                                            }
                                            opt.onSeek.call($object, where);
                                        }
                                    })
                                }
                            }
                        }
                    }

                    if (opt.txtSeeks.length > 0) {
                        var s_lists = [];
                        for (var j = 0; j < opt.txtSeeks.length; j++) {
                            for (var i = 0; i < opt.titles.length; i++) {
                                if (opt.titles[i].name == opt.txtSeeks[j]) {
                                    s_lists.push({
                                        name: opt.titles[i].name,
                                        ui_title: opt.titles[i].ui_title
                                    });
                                }
                            }
                        }
                        if (s_lists.length > 0) {
                            top_menus_r.push({
                                id: "s_field",
                                type: "combobox",
                                editable: false,
                                width: opt.isSimpleDDLW,

                                panelWidth: opt.isSimpleDDLW,
                                panelHeight: (33 * s_lists.length),
                                panelMaxHeight: 300,
                                valueField: 'name',
                                textField: 'ui_title',
                                data: s_lists,
                                value: s_lists[0].name
                            });

                            top_menus_r.push({
                                id: "s_value",
                                type: "textbox",
                                width: opt.isSimpleTxtW,
                                buttonIcon: 'yqwin-btn-icon fa fa-search',
                                iconAlign: 'left',
                                onClickButton: function () {
                                    var name = opt.menu_r.yqTopMenu("getValue", "s_field");
                                    var val = opt.menu_r.yqTopMenu("getValue", "s_value");
                                    var where = "";
                                    if (!(name === null) && !(val === null || val == "")) {
                                        where = name + " like '%" + val.replace("'", "''") + "%'";
                                    }

                                    opt.onSeek.call($object, where);
                                }
                            });
                        }
                    }
                }

                if (opt.isSeek) {
                    top_menus_r.push({
                        id: "search",
                        mini: false,
                        text: opt.searchText,
                        iconCls: opt.type == 1 ? "yqwin-btn-icon fa fa-search-plus" : "yqwin-btn-icon-sub fa fa-search-plus",
                        onClick: function (e) {
                            //e.preventDefault();
                            show_seek($object, this);

                        }
                    })
                }
            }

            opt.links_r.forEach(menu => {
                if (typeof menu.id != "undefined") {
                    if (menu.iconCls === undefined || menu.iconCls == null || menu.iconCls == "") {
                        menu.iconCls = "icon2-more";
                    }

                    if (typeof menu.onClick != "function") {
                        menu.onClick = function () {
                            opt.onClick.call($object, menu.id);
                        }
                    }

                    top_menus_r.push(menu);
                } else {
                    top_menus_r.push({});
                }
            });

            opt.top_menus_r = top_menus_r;
        }

        opt.menu_l = $('<div></div>');
        opt.menu_r = $('<div></div>');
        if (opt.type == 2) {  //弹出菜单
            $object.append(opt.menu_l);
            //$object.append(opt.menu_r);
        }
        else {
            var $table = $('<table width="100%"></table>');
            $object.append($table);
            var $tr = $("<tr></tr>");
            $table.append($tr);
            var $td1 = $('<td></td>');
            var $td2 = $('<td align="right" style="font-size: medium;"></td>');
            $tr.append($td1);
            $tr.append($td2);
            opt.menu_l = $('<div></div>');
            opt.menu_r = $('<div></div>');
            $td1.append(opt.menu_l);
            $td2.append(opt.menu_r);
        }

        opt.beforeBindingTitle.call($object, top_menus, pop_menus, function (_top_menus, _pop_menus) {
            opt.top_menus = _top_menus;
            opt.pop_menus = _pop_menus;
            if (opt.type == 2) {
                initmenu_2($object, function () {
                    form_load($object);
                });
            }
            else if (opt.type == 3) {
                initmenu_1($object, function () {
                    initmenu_2($object, function () {
                        initmenu_r($object, function () {
                            form_load($object);
                        });
                    });
                });
            }
            else {
                initmenu_1($object, function () {
                    initmenu_r($object, function () {
                        form_load($object);
                    });
                });
            }
        });
    }

    function initmenu_1($object, callback) {
        var opt = options($object);
        opt.menu_l.yqTopMenu({
            menus: opt.top_menus,
            mini: opt.mini,
            afterBindingTitle: function () {
                callback();
            },
        });
    }

    function initmenu_2($object, callback) {
        var opt = options($object);

        if (opt.pop_menus.length == 0) {
            callback();
            return;
        }

        $object.yqPopMenu({
            bind: opt.bind,
            afterBindingTitle: function () {
                callback();
            },

            width: opt.width,
            menus: opt.pop_menus,
            afterBindingTitle: function () {
                callback();
            }, //标题绑定后
            beforeOpen: function (_callback) {
                opt.beforeOpen.call($object, function (tf) {
                    _callback(tf);
                });
            },
        });
    }

    function initmenu_r($object, callback) {
        var opt = options($object);

        if (opt.top_menus_r.length > 0) {
            if (opt.type != 2) {
                opt.menu_r.yqTopMenu({
                    menus: opt.top_menus_r,
                    mini: opt.mini,
                    afterBindingTitle: function () {
                        callback();
                    },
                });
            }
        }
        else {
            callback();
        }
    }

    function getBtns($object, id) {
        var opt = options($object);
        var btns = [];
        if (opt.type == 2) {
            if (opt.pop_menus.length > 0) {
                var btn = $object.yqPopMenu("getBtn", id);
                if (!(btn === null)) {
                    btns.push(btn);
                }
            }
        } else if (opt.type == 3) {
            var btn = opt.menu_l.yqTopMenu("getBtn", id);
            if (!(btn === null)) {
                btns.push(btn);
            }
            if (opt.top_menus_r.length > 0) {
                btn = opt.menu_r.yqTopMenu("getBtn", id);
                if (!(btn === null)) {
                    btns.push(btn);
                }
            }

            if (opt.pop_menus.length > 0) {
                btn = $object.yqPopMenu("getBtn", id);
                if (!(btn === null)) {
                    btns.push(btn);
                }
            }
        } else {
            var btn = opt.menu_l.yqTopMenu("getBtn", id);
            if (!(btn === null)) {
                btns.push(btn);
            }
            if (opt.top_menus_r.length > 0) {
                btn = opt.menu_r.yqTopMenu("getBtn", id);
                if (!(btn === null)) {
                    btns.push(btn);
                }
            }
        }

        return btns;
    }

    function isEdit($object, id, tf) {
        var opt = options($object);
        if (opt.type == 2) {
            if (opt.pop_menus.length > 0) {
                if (tf) {
                    $object.yqPopMenu("enable", id);
                }
                else {
                    $object.yqPopMenu("disable", id);
                }
            }
        }
        else if (opt.type == 3) {
            if (tf) {
                opt.menu_l.yqTopMenu("enable", id);
                if (opt.top_menus_r.length > 0) {
                    opt.menu_r.yqTopMenu("enable", id);
                }

                if (opt.pop_menus.length > 0) {
                    $object.yqPopMenu("enable", id);
                }
            }
            else {
                opt.menu_l.yqTopMenu("disable", id);
                if (opt.top_menus_r.length > 0) {
                    opt.menu_r.yqTopMenu("disable", id);
                }

                if (opt.pop_menus.length > 0) {
                    $object.yqPopMenu("disable", id);
                }
            }
        }
        else {
            if (tf) {
                opt.menu_l.yqTopMenu("enable", id);
                if (opt.top_menus_r.length > 0) {
                    opt.menu_r.yqTopMenu("enable", id);
                }
            }
            else {
                opt.menu_l.yqTopMenu("disable", id);
                if (opt.top_menus_r.length > 0) {
                    opt.menu_r.yqTopMenu("disable", id);
                }
            }
        }
    }

    function isShow($object, id, tf) {
        var opt = options($object);
        if (opt.type == 2) {
            if (opt.pop_menus.length == 0) {
                if (tf) {
                    $object.yqPopMenu("show", id);
                }
                else {
                    $object.yqPopMenu("hide", id);
                }
            }
        }
        else if (opt.type == 3) {
            if (tf) {
                opt.menu_l.yqTopMenu("show", id);
                if (opt.pop_menus.length == 0) {
                    $object.yqPopMenu("show", id);
                }
            }
            else {
                opt.menu_l.yqTopMenu("hide", id);
                if (opt.pop_menus.length == 0) {
                    $object.yqPopMenu("hide", id);
                }
            }
        }
        else {
            if (tf) {
                opt.menu_l.yqTopMenu("show", id);
            }
            else {
                opt.menu_l.yqTopMenu("hide", id);
            }
        }
    }

    function show_seek($object, e) {
        var opt = options($object);
        var prevent_e = $.getPreventDefault(e);
        //console.log("prevent_e", prevent_e);

        prevent_e.clientX = prevent_e.clientX - opt.seekModeW + 93;
        if (prevent_e.clientX < 0) {
            prevent_e.clientX = 0;
        }
        prevent_e.parent = $object;
        var mdiinfo = $.yqGetWindParams();
        var _pops = {
            prevent_e: prevent_e,
            mdiinfo: mdiinfo,
            showTitle: false,
            title: "测试",
            width: opt.seekModeW,
            height: opt.seekModeH,
            resizable: true,
            titles: opt.titles,

            buttons: [{
                text: "确定",
                iconCls: 'icon-ok',
                onClick: function ($body) {
                    var res = getSeekSql($object);
                    console.log(res);
                    opt.onSeek.call($object, res.where, res.order);
                    this.dialog("close");
                }
            }],
            onOpen: getSeekInfo,
        };

        if (window.top == window.self) {
            $.yqDialog(_pops);
        }
        else {
            window.top.$.yqDialog(_pops);
        }

        opt.onSeek.call($object, "");
    }

    function getSeekInfo($body) {
        var d_opt = $.yqDialog_fun.call(this, "options");

        var opt = options(d_opt.prevent_e.parent);
        var $uniqueID = d_opt.prevent_e.parent.attr("id");

        var $inner = $(`
            <table class="seektb_main" style="width:100%;">
                <tr>
                    <td style="width:25%;height:28px;background-color: #f4f4f4;">名称</td>
                    <td style="width:35%;background-color: #f4f4f4;">条件</td>
                    <td style="width:20%;background-color: #f4f4f4;">排序</td>
                    <td rowspan="2" style="vertical-align: top;text-align: left;padding: 0px 8px;">
                        <br>
                        <br>
                        %&nbsp; 任何值
                        <br>
                        !&nbsp;&nbsp;&nbsp; 没有值
                        <br>
                        <br>
                        <br>
                        默认查询为精确查询，模糊查询请输入%
                    </td>
                </tr>
                <tr>
                    <td colspan="3" style="vertical-align: top;">
                        <div style="overflow:auto;height: `+ ($body.height() - 72) + `px;">
                            <table class="tb_body seektb" width="100%;">
                            </table>
                        <div>
                    </td>
                </tr>
            </table>
        `);
        $body.append($inner);

        var $tb_body;
        if (window.top == window.self) {
            $tb_body = $inner.find(".tb_body");
        } else {
            $tb_body = window.top.$("#" + $body.attr("id")).find(".tb_body");
        }

        opt.seek_tb = $tb_body;

        loadData();

        function loadData() {
            var rows = [];
            var _row = {};

            for (var i = 0; i < opt.titles.length; i++) {
                var title = opt.titles[i];
                if (!title.is_func && title.ui_visible &&
                    title.ui_type <= 12 &&
                    title.name != "c_userid" && title.name != "m_userid"
                ) {
                    var $input = ``;
                    if (title.ui_type == 4 || title.ui_type == 9 || title.ui_type == 10 ||
                        title.ui_type == 11 || title.ui_type == 12) {
                        $input = `<td style="width:22%;">
                                    <input class="select_value_f" type="text" style="width:100%;height:28px;"/>
                                </td>
                                <td style="width:2%;text-align: center;">
                                    -
                                </td>
                                <td style="width:22%;">
                                    <input class="select_value_t" type="text" style="width:100%;height:28px;"/>
                                </td>`;
                    } else {
                        $input = `<td colspan="3" style="width:46%;">
                                    <input class="select_value" type="text" style="width:100%;height:28px;"/>
                                </td>`;
                    }

                    var $tr = $(`<tr class="_` + title.name + `" style="border-color:#ccc;">
                                    <td style="width:32%;">`+ title.ui_title + `</td>
                                    `+ $input + `
                                    <td>
                                        <select class="select_order" style="width:100%;height:28px;">
                                        </select>
                                    </td>
                                </tr>`);
                    $tb_body.append($tr);
                }
            }

            for (var i = 0; i < opt.titles.length; i++) {
                var title = opt.titles[i];
                if (!title.is_func && title.ui_visible &&
                    title.ui_type <= 12 &&
                    title.name != "c_userid" && title.name != "m_userid") {
                    if (title.ui_type == 1) {   //label
                        $tb_body.find("._" + title.name + " .select_value").textbox({});
                    }
                    else if (title.ui_type == 2) {   //textbox
                        $tb_body.find("._" + title.name + " .select_value").textbox({});
                    }
                    else if (title.ui_type == 3) {   //checkbox                   
                        $tb_body.find("._" + title.name + " .select_value").combobox({
                            valueField: 'id',
                            textField: 'text',
                            data: [{
                                "id": -1,
                                "text": "--请选择--",
                                "selected": true
                            }, {
                                "id": 1,
                                "text": "是"
                            }, {
                                "id": 0,
                                "text": "否"
                            }],
                            editable: false,
                            panelHeight: 'auto',
                            panelMaxHeight: 154
                        });
                    }
                    else if (title.ui_type == 4) {   //datebox
                        var $txt_f = $tb_body.find("._" + title.name + " .select_value_f");
                        var txt_w = $txt_f.parent().width() - 1;
                        $txt_f.datebox({
                            format: title.ui_mask.format,
                            formatter: function (date) {
                                if (date === null) {
                                    return "";
                                }
                                else {
                                    return $.formatterdatebox(date, $(this).datebox("options").format);
                                }
                            },
                            parser: function (s) {
                                if (s === undefined || s == "" || s === null) {
                                    return null;
                                }
                                return $.convert_todate(s);
                            },
                            width: txt_w
                        });
                        $tb_body.find("._" + title.name + " .select_value_t").datebox({
                            format: title.ui_mask.format,
                            formatter: function (date) {
                                if (date === null) {
                                    return "";
                                }
                                else {
                                    return $.formatterdatebox(date, $(this).datebox("options").format);
                                }
                            },
                            parser: function (s) {
                                if (s === undefined || s == "" || s === null) {
                                    return null;
                                }
                                return $.convert_todate(s);
                            },
                            width: txt_w
                        });
                    }
                    else if (title.ui_type == 5) {   //richbox
                        $tb_body.find("._" + title.name + " .select_value").textbox({});
                    }
                    else if (title.ui_type == 6) {   //combobox
                        var lists = [];
                        for (let j = 0; j < title.ui_mask.lists.length; j++) {
                            lists.push($.extend({}, title.ui_mask.lists[j]));
                        }
                        lists.unshift({ no: -1, name: "--请选择--" });
                        $tb_body.find("._" + title.name + " .select_value").combobox({
                            data: lists,
                            value: -1,
                            valueField: "no",
                            textField: "name",
                            editable: false,
                            panelHeight: 'auto',
                            panelMaxHeight: 154,
                        });
                    }
                    else if (title.ui_type == 7) {   //foreignkey
                        $tb_body.find("._" + title.name + " .select_value").yqForeignKey({
                            ajaxUrl: opt.ajaxUrl,
                            panelWidth: title.ui_mask.panelWidth,
                            panelHeight: title.ui_mask.panelHeight,

                            displayType: title.ui_mask.displayType,
                            dbname: title.ui_mask.dbName,
                            tbname: title.ui_mask.tbName,
                            tempno: title.ui_mask.tempNo,
                            addWhere: title.ui_mask.addWhere,
                            order: title.ui_mask.order,

                            defaultValue: null,

                            isPage: title.ui_mask.isPage,
                            pageSize: title.ui_mask.pageSize,
                            pageNo: 1,
                            pageList: title.ui_mask.pageList === undefined ? [5, 10, 20, 30, 40, 50] : title.ui_mask.pageList,

                            valueMember: title.ui_mask.valueMember,
                            displayMember: title.ui_mask.displayMember,

                            titles: [],

                            expandMember: title.ui_mask.expandMember === undefined ? null : title.ui_mask.expandMember,
                            seekNo: title.ui_mask.seekNo,
                            seekModeW: title.ui_mask.seekModeW,
                            seekModeH: title.ui_mask.seekModeH,
                        });
                    }
                    else if (title.ui_type == 8) {   //treeselect
                        var lists = [{ no: -1, name: "--请选择--" }];
                        if (title.ui_mask.level == 1) {
                            for (let j = 0; j < title.ui_mask.trees.length; j++) {
                                lists.push($.extend({}, title.ui_mask.trees[j]));
                            }
                        }
                        var $txt = $tb_body.find("._" + title.name + " .select_value");
                        var opts = {
                            data: lists,
                            value: -1,
                            valueField: "no",
                            textField: "name",
                            editable: false,
                            panelHeight: 'auto',
                            panelMaxHeight: 154,
                            o_options: title,
                        };

                        if (title.ui_mask.level >= 1 && title.ui_mask.subfield != "") {
                            opts.onChange = function (newValue, oldValue) {
                                var o_ = $(this).combobox("options");
                                var s_trees = [];
                                if (newValue != "-1") {
                                    var o_trees = $(this).combobox("getData");
                                    for (let j = 0; j < o_trees.length; j++) {
                                        if (newValue == o_trees[j].no) {
                                            if (!(o_trees[j].subs === undefined)) {
                                                s_trees = o_trees[j].subs;
                                            }
                                            break;
                                        }
                                    }
                                }
                                var $sub_txt = $tb_body.find("._" + o_.o_options.ui_mask.subfield + " .select_value");
                                if ($sub_txt.length == 0) {
                                    return;
                                }
                                var o_lists = [{ no: -1, name: "--请选择--" }];
                                for (let j = 0; j < s_trees.length; j++) {
                                    o_lists.push($.extend({}, s_trees[j]));
                                }
                                $sub_txt.combobox("loadData", o_lists);
                                $sub_txt.combobox("select", -1);
                            }
                        }

                        $txt.combobox(opts);
                    }
                    else if (title.ui_type == 9) {   //numberbox
                        var $txt_f = $tb_body.find("._" + title.name + " .select_value_f");
                        var txt_w = $txt_f.parent().width() - 1;
                        $txt_f.numberbox({
                            precision: title.ui_mask.precision,
                            width: txt_w
                        });
                        $tb_body.find("._" + title.name + " .select_value_t").numberbox({
                            precision: title.ui_mask.precision,
                            width: txt_w
                        });
                    }
                    else if (title.ui_type == 10) {   //numberspinner 
                        var $txt_f = $tb_body.find("._" + title.name + " .select_value_f");
                        var txt_w = $txt_f.parent().width() - 1;
                        $txt_f.numberspinner({
                            width: txt_w
                        });
                        $tb_body.find("._" + title.name + " .select_value_t").numberspinner({
                            width: txt_w
                        });
                    }
                    else if (title.ui_type == 11) {   //timespinner
                        var $txt_f = $tb_body.find("._" + title.name + " .select_value_f");
                        var txt_w = $txt_f.parent().width() - 1;
                        $txt_f.timespinner({
                            width: txt_w
                        });
                        $tb_body.find("._" + title.name + " .select_value_t").timespinner({
                            width: txt_w
                        });
                    }
                    else if (title.ui_type == 12) {   //datetimebox
                        var $txt_f = $tb_body.find("._" + title.name + " .select_value_f");
                        var txt_w = $txt_f.parent().width() - 1;
                        $txt_f.datetimebox({
                            width: txt_w,
                            format: title.ui_mask.format,
                            formatter: function (date) {
                                if (date === null) {
                                    return "";
                                }
                                else {
                                    return $.formatterdatetimebox(date, $(this).datebox("options").format);
                                }
                            },
                        });
                        $tb_body.find("._" + title.name + " .select_value_t").datetimebox({
                            width: txt_w, format: title.ui_mask.format,
                            formatter: function (date) {
                                if (date === null) {
                                    return "";
                                }
                                else {
                                    return $.formatterdatetimebox(date, $(this).datebox("options").format);
                                }
                            },
                        });
                    }

                    var $s_order = $tb_body.find("._" + title.name + " .select_order");
                    $s_order.combobox({
                        valueField: 'id',
                        textField: 'text',
                        data: [{
                            "id": -1,
                            "text": "-选择-",
                            "selected": true
                        }, {
                            "id": 1,
                            "text": "正序"
                        }, {
                            "id": 0,
                            "text": "倒序"
                        }],
                        editable: false,
                        panelHeight: 'auto',
                        panelMaxHeight: 154,
                        width: $s_order.parent().width()
                    });
                }
            }
        }
    }

    function getSeekSql($object) {
        var opt = options($object);
        var $uniqueID = $object.attr("ID");
        var titles = opt.titles;

        var sqlwhere = "";
        var order = "";

        var $tb_body = opt.seek_tb;
        for (var i = 0; i < opt.titles.length; i++) {
            var title = opt.titles[i];
            var where = "";

            if (!title.is_func && title.ui_visible &&
                title.ui_type <= 12 &&
                title.name != "c_userid" && title.name != "m_userid") {
                if (title.ui_type == 1) {   //label
                    var val = $tb_body.find("._" + title.name + " .select_value").textbox("getValue");
                    where = GetStringSql(title.name, val);
                }
                else if (title.ui_type == 2) {   //textbox
                    var val = $tb_body.find("._" + title.name + " .select_value").textbox("getValue");
                    where = GetStringSql(title.name, val);
                }
                else if (title.ui_type == 3) {   //checkbox
                    var val = $tb_body.find("._" + title.name + " .select_value").combobox("getValue");
                    if (val == "1") {
                        where = title.name + " = true";
                    }
                    else if (val == "0") {
                        where = title.name + " = false";
                    }
                }
                else if (title.ui_type == 4) {   //datebox
                    var val_f = $tb_body.find("._" + title.name + " .select_value_f").datebox("getValue");
                    var val_t = $tb_body.find("._" + title.name + " .select_value_t").datebox("getValue");
                    var val = "";

                    if (val_f != "") {
                        val_f = val_f + " 00:00:01";
                    }

                    if (val_t != "") {
                        val_t = val_t + " 23:59:59";
                    }

                    if (val_f != "" || val_t != "")
                        val = val_f + ".." + val_t;

                    if (val != "") {
                        where = GetDateTimeSql(title.name, val);
                    }
                }
                else if (title.ui_type == 5) {   //richbox
                    var val = $tb_body.find("._" + title.name + " .select_value").textbox("getValue");
                    where = GetStringSql(title.name, val);
                }
                else if (title.ui_type == 6) {   //combobox       
                    var val = $tb_body.find("._" + title.name + " .select_value").combobox("getValue");
                    if (val != "-1") {
                        where = title.name + " = " + val;
                    }
                }
                else if (title.ui_type == 7) {   //foreignkey
                    var val = $tb_body.find("._" + title.name + " .select_value").yqForeignKey("getValue");
                    where = GetStringSql(title.name, val);
                }
                else if (title.ui_type == 8) {   //treeselect
                    var val = $tb_body.find("._" + title.name + " .select_value").combobox("getValue");
                    if (val != "-1") {
                        where = title.name + " = " + val;
                    }
                }
                else if (title.ui_type == 9) {   //numberbox
                    var val_f = $tb_body.find("._" + title.name + " .select_value_f").numberbox("getValue");
                    var val_t = $tb_body.find("._" + title.name + " .select_value_t").numberbox("getValue");
                    var val = "";

                    if (val_f != "" || val_t != "")
                        val = val_f + ".." + val_t;

                    if (val != "") {
                        where = GetNumberSql(title.name, val);
                    }
                }
                else if (title.ui_type == 10) {   //numberspinner 
                    var val_f = $tb_body.find("._" + title.name + " .select_value_f").numberspinner("getValue");
                    var val_t = $tb_body.find("._" + title.name + " .select_value_t").numberspinner("getValue");
                    var val = "";

                    if (val_f != "" || val_t != "")
                        val = val_f + ".." + val_t;

                    if (val != "") {
                        where = GetNumberSql(title.name, val);
                    }
                }
                else if (title.ui_type == 11) {   //timespinner
                    var val_f = $tb_body.find("._" + title.name + " .select_value_f").timespinner("getValue");
                    var val_t = $tb_body.find("._" + title.name + " .select_value_t").timespinner("getValue");
                    var val = "";

                    if (val_f != "" || val_t != "")
                        val = val_f + ".." + val_t;

                    if (val != "") {
                        where = GetDateTimeSql(title.name, val);
                    }
                }
                else if (title.ui_type == 12) {   //datetimebox
                    var val_f = $tb_body.find("._" + title.name + " .select_value_f").datetimebox("getValue");
                    var val_t = $tb_body.find("._" + title.name + " .select_value_t").datetimebox("getValue");
                    var val = "";

                    if (val_f != "" || val_t != "")
                        val = val_f + ".." + val_t;

                    if (val != "") {
                        where = GetDateTimeSql(title.name, val);
                    }
                }

                if (where != "") {
                    if (sqlwhere == "")
                        sqlwhere = where;
                    else
                        sqlwhere = sqlwhere + " and " + where;
                }

                var ordselect = $tb_body.find("._" + title.name + " .select_order").combobox("getValue");
                if (ordselect == "0") {
                    order += (order == "" ? "" : ",") + title.name + " desc";
                }
                else if (ordselect == "1") {
                    order += (order == "" ? "" : ",") + title.name;
                }
            }
        }

        return {
            "where": sqlwhere,
            "order": order
        };

        //字符串
        function GetStringSql(iD, value) {
            try {
                if (val === null || val === undefined || val == "") {
                    return "";
                }

                if (value.indexOf("%") != -1) {
                    return iD + " like '" + value + "'";
                }
                else if (value.indexOf("!") == 0 && value.length == 1) {
                    return iD + " is NULL";
                }
                else {
                    return iD + " = '" + value + "'";
                }
            }
            catch (Error) {
                return "";
            }
        }

        //数字
        function GetNumberSql(iD, value) {
            try {
                try {
                    if (value.indexOf("..") > 0) {
                        var tmp = value.split('..');

                        if (tmp[0] != "" && tmp[1] != "")
                            return iD + " between " + tmp[0] + " and " + tmp[1];
                        else if (tmp[0] != "")
                            return iD + ">=" + tmp[0];
                        else if (tmp[0] != "")
                            return iD + "<=" + tmp[1];
                    }
                }
                catch (Error) {
                    return "";
                }
            }
            catch (Error) {
                return "";
            }
        }

        //日期
        function GetDateTimeSql(iD, value) {
            try {
                if (value.indexOf("..") > 0) {
                    var tmp = value.split('..');

                    if (tmp[0] != "" && tmp[1] != "")
                        return iD + " between '" + tmp[0] + "' and '" + tmp[1] + "'";
                    else if (tmp[0] != "")
                        return iD + ">='" + tmp[0] + "'";
                    else if (tmp[0] != "")
                        return iD + "<='" + tmp[1] + "'";
                }
            }
            catch (Error) {
                return "";
            }
        }
    }

})(jQuery);